var $report = (function () {
    var wordObj = null;
    /*私有方法 获取全路径*/
    function getRootPath() {
        var location = document.location;
        if("file:" == location.protocol) {
            var str = location.toString();
            return str.replace(str.split("/").reverse()[0], "");
        }
        var pathName = location.pathname.split("/");
        return location.protocol + "//" + location.host + "/" + pathName[1] + "/";
    }

    /*私有方法 获取DsoFramer对象*/
    function getwordObj(oframeid, wordpath) {
        if(wordObj == null) {
            wordObj = document.getElementById(oframeid);
            if(wordObj == null || wordObj.Caption != "Office Framer Control") {
                alert("加载组件出错，请先注册组件");
                return;
            }
            wordObj.Titlebar = false;
            wordObj.Toolbars = false;
            wordObj.Menubar = false;
            wordObj.open(getRootPath() + wordpath, true);
            wordObj.ActiveDocument.Protect(1);
        }
        return wordObj;
    }

    /*私有方法 根据名称替换书签*/
    function replaceBookmark(strName, content, type) {
        try {
            wordObj.ActiveDocument.Unprotect();
        } catch(e) {
        }
        if(wordObj.ActiveDocument.BookMarks.Exists(strName)) {
            if(type != null && type == "pic") {//图片
                var objDoc = wordObj.ActiveDocument.BookMarks(strName).Range.Select();
                var objSelection = wordObj.Selection;
                objSelection.TypeParagraph();
                var objShape = objSelection.InlineShapes.AddPicture(getRootPath() + content);
            }
            else {
                wordObj.ActiveDocument.BookMarks(strName).Range.Select();
                wordObj.ActiveDocument.Application.selection.Text = content;
            }
        }
        wordObj.ActiveDocument.Protect(1);
    }

    function exitPreview() {
        wordObj.PrintPreviewExit();
    }

    function baseVoListObj() {
        this.singlevo = null;
        this.volist = [];
        this.cols = [];
        this.captions = [];
        this.widths = [];
    }

    /*私有方法 删除所有书签*/
    function clearAllBookMarksValue() {
        while(wordObj.ActiveDocument.Bookmarks.Count != 0) {
            wordObj.ActiveDocument.Bookmarks(1).Delete();
        }
    }

    return {

        //	接口方法 第一个参数可以接收数组 或者是对象，第二个参数是要生成的form的 名字
        jsonToForm : function (json, name) {

            if('array' == $type(json)) {
                json.each(function(v, i) {
                    createOneForm(v);
                });
            } else if('object' == $type(json)) {
                createOneForm(json);
            } else {
                return false;
            }
            return true;
            function createOneForm(oneForm) {
                var form = new Element('form', {
                    'id':name,
                    'name':name,
                    'styles':{'display':'none'}
                }).inject(window.document.body, 'bottom');
                for(e in oneForm) {
                    var value = $defined(oneForm[e].value) ? oneForm[e].value : oneForm[e];
                    value = value || '';
                    new Element('textarea', {
                        'name':e,
                        'text': value
                    }).inject(form, 'bottom');
                }
            }

        },

        /*
         test : function(){
         try{wordObj.ActiveDocument.Unprotect();}catch(e){}

         wordObj.ActiveDocument.Select();
         wordObj.ActiveDocument.Application.selection.Copy();
         wordObj.ActiveDocument.Application.selection.Collapse(0);
         //clearAllBookMarksValue();
         alert(wordObj.ActiveDocument.Bookmarks.Count);
         wordObj.ActiveDocument.Application.selection.MoveEnd();
         wordObj.ActiveDocument.Application.selection.InsertBreak();
         wordObj.ActiveDocument.Application.selection.Paste();
         alert(wordObj.ActiveDocument.Bookmarks.Count);

         },
         */
        /*公共方法  批量生成*/
        batchprint:function(volistarray, oframeid, wordpath, beforeprint, afterprint) {
            if(beforeprint) beforeprint.call();
            try {
                wordObj.ActiveDocument.Unprotect();
            } catch(e) {
            }
            exitPreview();
            var len = volistarray.length,i = 0;
            wordObj.ActiveDocument.Select();
            wordObj.ActiveDocument.Application.selection.Copy();
            for(i; i < len; i++) {
                if(i == 0) {
                    this.replaceBookmark(volistarray[i]);
                }
                try {
                    wordObj.ActiveDocument.Unprotect();
                } catch(e) {
                }
                clearAllBookMarksValue();
                if(i != 0) {
                    wordObj.ActiveDocument.Select();
                    wordObj.ActiveDocument.Application.selection.Collapse(0);
                    wordObj.ActiveDocument.Application.selection.MoveEnd();
                    wordObj.ActiveDocument.Application.selection.InsertBreak(2);
                    wordObj.ActiveDocument.Application.selection.Paste();
                    this.replaceBookmark(volistarray[i]);
                }
            }
            try {
                wordObj.ActiveDocument.Unprotect();
            } catch(e) {
            }
            wordObj.ActiveDocument.Fields.Update();
            wordObj.ActiveDocument.Protect(1);
            if(afterprint) afterprint.call();
        },

        /*公共方法  批量生成*/
        batchBuild:function(volistarray, oframeid, wordpath, beforeprint, afterprint) {
            if(beforeprint) beforeprint.call();
            try {
                wordObj.ActiveDocument.Unprotect();
            } catch(e) {
            }
            exitPreview();
            var len = volistarray.length,i = 0;
            wordObj.ActiveDocument.Select();
            wordObj.ActiveDocument.Application.selection.Copy();
            for(i; i < len; i++) {
                if(i == 0) {
                    this.replaceBookmark(volistarray[i]);
                }
                try {
                    wordObj.ActiveDocument.Unprotect();
                } catch(e) {
                }
                clearAllBookMarksValue();
                if(i != 0) {
                    wordObj.ActiveDocument.Select();
                    wordObj.ActiveDocument.Application.selection.Collapse(0);
                    wordObj.ActiveDocument.Application.selection.MoveEnd();
                    wordObj.ActiveDocument.Application.selection.InsertBreak(2);
                    wordObj.ActiveDocument.Application.selection.Paste();
                    this.replaceBookmark(volistarray[i]);
                }
            }
            try {
                wordObj.ActiveDocument.Unprotect();
            } catch(e) {
            }
            wordObj.ActiveDocument.Fields.Update();
            wordObj.ActiveDocument.Protect(1);
            if(afterprint) afterprint.call();
        },
        /*公共方法  关闭*/
        close :function() {
//            if(wordObj != null) {
//                wordObj.Quit();
//            }
        	//word打印默认都是弹出页面打印  关闭直接调用 box.close();
        	var box = parent[name];
        	if($chk(box))box.close();
        },
        /*公共方法  根据表格模板构造表格*/
        replaceBookmarkbuildtable:function(volist) {
            exitPreview();
            try {
                wordObj.ActiveDocument.Unprotect();
            } catch(e) {
            }
            var firstbookmark = volist.cols[0];
            if(wordObj.ActiveDocument.Bookmarks.Exists(firstbookmark)) {
                var colindex = wordObj.ActiveDocument.Bookmarks(firstbookmark).Range.Cells(1).ColumnIndex;
                var rowindex = wordObj.ActiveDocument.Bookmarks(firstbookmark).Range.Cells(1).RowIndex;
                var colcount = volist.cols.length;
                var rowcount = volist.volist.length;

                var currenttable = wordObj.ActiveDocument.Bookmarks(firstbookmark).Range.Tables(1);
                currenttable.Cell(rowindex, colindex).Select();
                var i = 0,j = 0;
                for(i; i < rowcount - 1; i++) {
                    wordObj.ActiveDocument.Application.selection.InsertRowsAbove();
                }
                var temprc = rowcount + rowindex;
                var tempcc = colcount + colindex;

                var m = 0,n = 0;
                for(i = rowindex; i < temprc; i++) {
                    for(j = colindex; j < tempcc; j++) {
                        var temptext = volist.volist[m][volist.cols[n]];
                        currenttable.Cell(i, j).Range.InsertAfter(temptext);
                        //currenttable.Cell(i,j).Range.InsertAfter((i)+","+(j));
                        n += 1;
                    }
                    n = 0;
                    m += 1;
                }
            }
            wordObj.ActiveDocument.Fields.Update();
            wordObj.ActiveDocument.Protect(1);
        },
        /*公共方法 初始化*/
        init:function(oframeid, wordpath) {
            return getwordObj(oframeid, wordpath);
        },
        /*公共方法 打印预览*/
        printpreview : function () {
            wordObj.Titlebar = false;
            wordObj.Toolbars = true;
            wordObj.Menubar = false;
            wordObj.PrintPreview();
        },
        /*公共方法 打印*/
        print : function (breforeprint, afterprint, flag) {
            if(breforeprint) breforeprint.call();
            exitPreview();
            wordObj.PrintOut(flag);
            if(afterprint) afterprint.call();
        },
        /*公共方法 根据vo替换书签*/
        replaceBookmarkUsevo: function (voObj) {
            exitPreview();
            if(typeof voObj != "object") {
                alert("请输入正确的vo对象");
            } else {
                for(var i in voObj) {
                    replaceBookmark(i, voObj[i]);
                }
                wordObj.ActiveDocument.Fields.Update();
            }
        },
        /*公共方法 根据voList替换标签*/
        replaceBookmark:function(voList, tablebookmark, beforeprint, afterprint) {
            if(beforeprint) beforeprint.call();
            if(voList.singlevo != null) {
                this.replaceBookmarkUsevo(voList.singlevo);
            }
            if(tablebookmark) {
                this.replaceBookmarkUsevolist(tablebookmark, voList);
            } else {
                this.replaceBookmarkbuildtable(voList);
            }
            try {
                wordObj.ActiveDocument.Unprotect();
            } catch(e) {
            }
            wordObj.ActiveDocument.Fields.Update();
            wordObj.ActiveDocument.Protect(1);
            if(afterprint) afterprint.call();
        },

        /*公共方法 获取数据volist*/
        getVodata:function(singlevoformName, volistformName) {
            var voListObj = new baseVoListObj();
            var formObj = document.forms[singlevoformName];
            if(formObj) {
                var vo = {};
                if(formObj.elements) {
                    var elArray = formObj.elements;
                    for(var i = elArray.length; i >= 1;) {
                        i--;
                        vo[elArray[i].name] = elArray[i].value;
                    }
                    voListObj.singlevo = vo;
                }

            }
            if(volistformName) {
                volistformName.each(function(item, index) {
                    var formArray = document.getElementsByName(item);
                    var formObj = null;
                    if(formArray.length > 0)formObj = formArray[0];
                    if(formObj != null & formObj.elements) {
                        var vo = {};
                        var elArray = formObj.elements;
                        for(var i = elArray.length; i >= 0; i--) {
                            vo[elArray[i].name] = elArray[i].value;
                        }
                        voListObj.volist.push(vo);
                        if(index = 0) {
                            voListObj.cols.push(formObj.elements[0].name);
                            voListObj.captions.push(formObj.elements[0].caption ? formObj.elements[0].caption : "");
                            voListObj.widths.push(formObj.elements[0].style.width);
                        }
                    }
                });
            }
            return voListObj;
        },


        /*公共方法 根据Form表单元素构造vo*/
        getSingleVo: function (formName, arrayObj) {//第二个参数可以为空，不填时默认为表单里的所有元素
            var formObj = document.forms[formName];
            if(formObj != null) {
                if(arrayObj != null) {
                    if(arrayObj instanceof Array) {
                        var vo = {};
                        for(var i = 0; i < arrayObj.length; i++) {
                            if(formObj.elements[arrayObj[i]] != undefined) {
                                vo[arrayObj[i]] = formObj.elements[arrayObj[i]].value;
                            }
                        }
                        return vo;
                    } else {
                        alert("弟二个参数应为数组类型");
                    }
                } else {
                    var vo = {};
                    for(var i = 0; i < formObj.elements.length; i++) {
                        vo[formObj.elements[i].name] = formObj.elements[i].value;
                    }
                    return vo;
                }
            } else {
                alert("第一个参数表示的表单不存在");
                return null;
            }
        },
        /*公共方法 根据Form表单元素构造vo*/
        getVoList :function(formName, arrayObj) {//表单名，属性数组(可以为空)
            /*ar formArray = document.forms[formName];*/
            var formArray = document.getElementsByName(formName);
            if(formArray != null) {
                if(arrayObj instanceof Array) {
                    var voListObj = new baseVoListObj();
                    /*for(var i=0;i<formArray.length;i++){
                     var vo = {};
                     for(var j=0;j<arrayObj.length;j++){
                     if(formArray[i].elements[arrayObj[j]]!= undefined ){
                     vo[arrayObj[j]] =  formArray[i].elements[arrayObj[j]].value;
                     if(i==0){
                     voListObj.captions.push( formArray[i].elements[arrayObj[j]].caption? formArray[i].elements[arrayObj[j]].caption:"");
                     voListObj.cols.push(arrayObj[j]);
                     voListObj.widths.push(formArray[i].elements[arrayObj[j]].style.width);
                     }
                     }
                     }
                     voListObj.singlevo.push(vo);
                     }*/
                    var vo = {};
                    if(formArray[0].elements) {
                        arrayObj.each(function(item, index) {
                            vo[item] = formArray[0].elements[item].value;
                        });
                    }
                    voListObj.singlevo = vo;
                    return voListObj;
                } else {
                    var voListObj = new baseVoListObj();
                    for(var i = 0; i < formArray.length; i++) {
                        var vo = {};
                        for(var j = 0; j < formArray[i].elements.length; j++) {
                            vo[formArray[i].elements[j].name] = formArray[i].elements[j].value;
                            if(i == 0) {
                                voListObj.cols.push(formArray[i].elements[j].name);
                                voListObj.captions.push(formArray[i].elements[j].caption ? formArray[i].elements[j].caption : "");
                                voListObj.widths.push(formArray[i].elements[j].style.width);
                            }
                        }
                        voListObj.volist.push(vo);
                    }
                    return voListObj;
                }
            } else {
                return null;
            }
        },


        /*公共方法 根据volist替换书签*/
        replaceBookmarkUsevolist:function(strName, voListObj, isBorder) {
            try {
                wordObj.ActiveDocument.Unprotect();
            } catch(e) {
            }
            if(typeof voListObj != "object") {
                alert("参数应为数组类型");
            } else {
                var row = voListObj.volist.length;
                var col = voListObj.cols.length;
                if(row != 0 && col != 0) {
                    var objDoc = wordObj.ActiveDocument.BookMarks(strName).Range;
                    var objTable = wordObj.ActiveDocument.Tables.Add(objDoc, row + 1, col);//插入表格
                    for(var k = 0; k < voListObj.captions.length; k++) {
                        objTable.Cell(1, k + 1).Range.InsertAfter(voListObj.captions[k]);
                    }
                    for(var i = 0; i < row; i++) {
                        for(var j = 0; j < col; j++) {
                            objTable.Cell(i + 2, j + 1).Range.InsertAfter(voListObj.volist[i][voListObj.cols[j]]);
                            var width = voListObj.widths[j];
                            if(width.indexOf("px") != -1) {
                                objTable.Cell(i + 2, j + 1).Width = (width.substr(0, width.length - 2) / 100) * 28.35;//1厘米=28.35磅
                            }
                        }
                    }
                    //objTable.AutoFormat(1);
                    if(false == isBorder) {
                        objTable.Borders.InsideLineStyle = 0;
                    } else {
                        objTable.Borders.InsideLineStyle = 1;
                    }
                    objTable.Borders.OutsideLineStyle = 1;
                }
            }
            wordObj.ActiveDocument.Fields.Update();
            wordObj.ActiveDocument.Protect(1);
        }
        /************************end****************************************************/
    };
}());